package com.fmi.chesstern.gameboard;

public class GameField {
	
	private int row;
	private int column;
	
	public GameField(int row, int col) {
		this.row = row;
		this.column = col;
	}
	
	public GameField(String str) {
		char[] chars = str.toCharArray();
		this.column = translateFromLetter(chars[0]);
		this.row = Integer.parseInt(str.substring(1));
	}
	
	public GameField(GameField other) {
		this(other.getRow(), other.getColumn());
	}
	
	public static boolean isFieldCorrect(GameField field) {
		boolean isCorrect = false;
		int row = field.getRow();
		int col = field.getColumn();
		if (row >= 0 && row <= 7 && col >= 0 && col <= 7)
			isCorrect = true;
		return isCorrect;
	}
	
	public int getRow() {
		return row;
	}


	public void setRow(int row) {
		this.row = row;
	}


	public int getColumn() {
		return column;
	}


	public void setColumn(int column) {
		this.column = column;
	}
	
	private int translateFromLetter(char letter) {
		int value = -1;
		switch (letter) {
		case 'a': value = 0; break;
		case 'b': value = 1; break;
		case 'c': value = 2; break;
		case 'd': value = 3; break;
		case 'e': value = 4; break;
		case 'f': value = 5; break;
		case 'g': value = 6; break;
		case 'h': value = 7; break;
		}
		return value;
	}
	
	private String translateToLetter(int index) {
		String letter = "";
		switch (index) {
		case 0: letter = "a"; break;
		case 1: letter = "b"; break;
		case 2: letter = "c"; break;
		case 3: letter = "d"; break;
		case 4: letter = "e"; break;
		case 5: letter = "f"; break;
		case 6: letter = "g"; break;
		case 7: letter = "h"; break;
		}
		return letter;
	}
	
	public String toString() {
		return translateToLetter(column) + row;
	}
	
	@Override 
	public boolean equals(Object other) {
	    boolean result = false;
	    if (other instanceof GameField) {
	        GameField that = (GameField) other;
	        result = (this.getRow() == that.getRow() && this.getColumn() == that.getColumn());
	    }
	    return result;
	}

	
	
}
